package com.demo.widget.meis;

import com.demo.widget.BaseAbility;
import com.demo.widget.ResourceTable;
import com.meis.widget.spiderweb.SpiderWebView;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;

/**
 * Created by wenshi on 2019/3/27.
 * Description
 */
public class MeiSpiderWebAbility extends BaseAbility implements Slider.ValueChangedListener {
    private SpiderWebView mSpiderWebView;
    private ScrollView mDrawerLayout;
    private Component emptyView;
    private Image tools;
    private Image menuItem;
    private Slider mSeekbar1;
    private Slider mSeekbar2;
    private Slider mSeekbar3;
    private Slider mSeekbar4;
    private Text mTextView1;
    private Text mTextView2;
    private Text mTextView3;
    private Text mTextView4;
    private Text mTextView5;
    private Text mTextView6;
    private Text mTextView7;
    private Text mTextView8;
    private Slider mSeekbar5;
    private Slider mSeekbar6;
    private Slider mSeekbar7;
    private Slider mSeekbar8;
    private AnimatorValue animatorValue;
    private float width;
    private boolean isOpen = false;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_sipder_web);
        initView();
        initData();
        width = mDrawerLayout.getWidth();
        mDrawerLayout.setTranslationX(-width);
        initAnimator();
        initListener();

    }

    private void initData() {
        mSeekbar1.setProgressValue(49);
        mTextView1.setText("小点数量：" + 50);
        mSeekbar2.setProgressValue(4);
        mTextView2.setText("加速度： " + 7);
        mSeekbar3.setProgressValue(250);
        mTextView3.setText("最大连线距离： " + 270);
        mSeekbar4.setProgressValue(150);
        mTextView4.setText("连线透明度： " + 150);
        mSeekbar5.setProgressValue(2);
        mTextView5.setText("连线粗细： " + 2);
        mSeekbar6.setProgressValue(1);
        mTextView6.setText("小点半径： " + 1);
        mSeekbar7.setProgressValue(1);
        mTextView7.setText("触摸点半径： " + 1);
        mSeekbar8.setProgressValue(50);
        mTextView8.setText("引力强度： " + 50);
    }

    private void initAnimator() {
        animatorValue = new AnimatorValue();
        animatorValue.setCurveType(Animator.CurveType.PHYSICAL_FLING);
        animatorValue.setDuration(200);
        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                if (isOpen) { // 抽屉展开
                    tools.setRotation(v* 180);
                    emptyView.setAlpha(v);
                    mDrawerLayout.setTranslationX((v - 1F)*(width));
                } else {
                    tools.setRotation((1-v)*(-180));
                    emptyView.setAlpha(1-v);
                    mDrawerLayout.setTranslationX(v*(-width));
                }
            }
        });
        animatorValue.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                if (isOpen){
                    tools.setPixelMap(ResourceTable.Media_ic_right);
                }else {
                    tools.setPixelMap(ResourceTable.Media_ic_menu);
                }
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
    }


    private void initView() {
        mSpiderWebView = (SpiderWebView) findComponentById(ResourceTable.Id_cob_web_view);
        mDrawerLayout = (ScrollView) findComponentById(ResourceTable.Id_left_drawer);
        mTextView1 = (Text) findComponentById(ResourceTable.Id_Text1);
        mTextView2 = (Text) findComponentById(ResourceTable.Id_Text2);
        mTextView3 = (Text) findComponentById(ResourceTable.Id_Text3);
        mTextView4 = (Text) findComponentById(ResourceTable.Id_Text4);
        mTextView5 = (Text) findComponentById(ResourceTable.Id_Text5);
        mTextView6 = (Text) findComponentById(ResourceTable.Id_Text6);
        mTextView7 = (Text) findComponentById(ResourceTable.Id_Text7);
        mTextView8 = (Text) findComponentById(ResourceTable.Id_Text8);
        mSeekbar1 = (Slider) findComponentById(ResourceTable.Id_seekbar1);
        mSeekbar2 = (Slider) findComponentById(ResourceTable.Id_seekbar2);
        mSeekbar3 = (Slider) findComponentById(ResourceTable.Id_seekbar3);
        mSeekbar4 = (Slider) findComponentById(ResourceTable.Id_seekbar4);
        mSeekbar5 = (Slider) findComponentById(ResourceTable.Id_seekbar5);
        mSeekbar6 = (Slider) findComponentById(ResourceTable.Id_seekbar6);
        mSeekbar7 = (Slider) findComponentById(ResourceTable.Id_seekbar7);
        mSeekbar8 = (Slider) findComponentById(ResourceTable.Id_seekbar8);
        emptyView = findComponentById(ResourceTable.Id_empty_view);
        tools = (Image) findComponentById(ResourceTable.Id_tools);
        menuItem = (Image) findComponentById(ResourceTable.Id_menu_item1);
    }



    private void initListener() {
        mSeekbar1.setValueChangedListener(this);
        mSeekbar2.setValueChangedListener(this);
        mSeekbar3.setValueChangedListener(this);
        mSeekbar4.setValueChangedListener(this);
        mSeekbar5.setValueChangedListener(this);
        mSeekbar6.setValueChangedListener(this);
        mSeekbar7.setValueChangedListener(this);
        mSeekbar8.setValueChangedListener(this);
        emptyView.setClickedListener(component ->{
                    if (isOpen){
                        showDrawer();
                    }
                }
        );
        menuItem.setClickedListener(component -> {
            if (isOpen) {
                showDrawer();
            }
            mSpiderWebView.restart();
        });
        tools.setClickedListener(component -> showDrawer());

    }

    private void showDrawer() {
        isOpen = !isOpen;
        animatorValue.start();
    }

    @Override
    public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
        switch (slider.getId()) {
            case ResourceTable.Id_seekbar1:
                mSpiderWebView.setPointNum(progress + 1);
                mTextView1.setText("小点数量：" + mSpiderWebView.getPointNum());
                break;
            case ResourceTable.Id_seekbar2:
                mSpiderWebView.setPointAcceleration(progress + 3);
                mTextView2.setText("加速度： " + mSpiderWebView.getPointAcceleration());
                break;
            case ResourceTable.Id_seekbar3:
                mSpiderWebView.setMaxDistance(progress + 20);
                mTextView3.setText("最大连线距离： " + mSpiderWebView.getMaxDistance());
                break;
            case ResourceTable.Id_seekbar4:
                mSpiderWebView.setLineAlpha(progress);
                mTextView4.setText("连线透明度： " + mSpiderWebView.getLineAlpha());
                break;
            case ResourceTable.Id_seekbar5:
                mSpiderWebView.setLineWidth(progress);
                mTextView5.setText("连线粗细： " + mSpiderWebView.getLineWidth());
                break;
            case ResourceTable.Id_seekbar6:
                mSpiderWebView.setPointRadius(progress);
                mTextView6.setText("小点半径： " + mSpiderWebView.getPointRadius());
                break;
            case ResourceTable.Id_seekbar7:
                mSpiderWebView.setTouchPointRadius(progress);
                mTextView7.setText("触摸点半径： " + mSpiderWebView.getTouchPointRadius());
                break;
            case ResourceTable.Id_seekbar8:
                mSpiderWebView.setGravitation_strength(progress);
                mTextView8.setText("引力强度： " + mSpiderWebView.getGravitation_strength());
                break;
        }
    }

    @Override
    public void onTouchStart(Slider slider) {
    }

    @Override
    public void onTouchEnd(Slider slider) {
    }


}
